package scuts.core.macros;
import haxe.PosInfos;

import scuts.core.macros.F;
import utest.Assert;

/**
 * ...
 * @author Heinz Hölzer
 */

class FlazyTest 
{
  public function new() 
    {
      
    }
  
  function itEquals <T>(expected:Iterator<T>, actual:Iterator<T>, ?posInfos:PosInfos) 
    {
      ScutsAssert.assertIteratorEquals(expected, actual, posInfos);
    }
  
  
  public function testSimple () 
    {
      var expected = 0...4;
      var actual = F.lazy(x, x <= 0...4);
      
      itEquals(expected, actual);
      
    }
    
  public function testNested () 
    {
      var expected = [0,1,0,1].iterator();
      var actual = F.lazy(x, x <= 0...2, y <= 0...2);
      
      itEquals(expected, actual);
      
    }
    
  public function testMultiple () 
    {
      var expected = [[0, 0], [1, 1]].iterator();
      var actual = F.lazy([x1,x2], x1 <= 0...2 && x2 <= 0...2);
      itEquals(expected, actual);
    }
  
  public function testMultipleAndNested () 
    {
      var expected = [[0, 0], [1, 1], [0, 0], [1, 1]].iterator();
      var actual = F.lazy([x1,x2], x1 <= 0...2 && x2 <= 0...2, y <= 0...2);
      itEquals(expected, actual);
    }
    
  public function testGuard1 () 
    {
      var expected = [0,2,3].iterator();
      var actual = F.lazy(x, x <= 0...4 || x != 1);
      itEquals(expected, actual);
    }
    
  public function testNestedGuards () 
    {
      var expected = [1].iterator();
      var actual = F.lazy(x, x <= 0...2 || x != 0, y <= 0...2 || y != 0);
      itEquals(expected, actual);
    }
    
  public function testGeneratorsWithDifferentSize () 
    {
      var expected = [0,1].iterator();
      var actual = F.lazy(x, x <= 0...2 && y <= 0...100000);
      itEquals(expected, actual);
    }
    
  public function testIfOutputFunctionDoesntChangeLoopVar () 
    {
      var expected = [0,1,2,3].iterator();
      var actual = F.lazy( { var y = x; x = 17; y; }, x <= 0...4);
      itEquals(expected, actual);
    }
    
  public function testIfPredicateDoesntChangeLoopVar () 
    {
      var expected = [0, 1, 2, 3].iterator();
      var actual = F.lazy( x, x <= 0...4 || { x = 17; true;});
      itEquals(expected, actual);
    }
}